import time
from datetime import datetime, timedelta


def get_date_list_between_datetime(start: datetime=None, end: datetime=None):
    if not start:
        start = time.get_date_zero_time()
    if not end:
        end = datetime.now()
    tdt: timedelta = end - start
    date_list = []
    for i in range(0, tdt.days):
        pass

def get_n_days_before_time(days):
    '''获取N天前时间'''
    return datetime.now() - timedelta(days=days)

def get_ndays_before_zero_time(days=0, ret_timestamp=False):
    now = datetime.now()
    nday_zero_time = now - timedelta(hours=now.hour, minutes=now.minute, seconds=now.second, microseconds=now.microsecond)
    #nday_zero = None
    if days:
        delta = timedelta(days=days)
        nday_zero_time = nday_zero_time - delta
    if ret_timestamp:
        nday_zero_time = int(nday_zero_time.timestamp() * 1000)
    return nday_zero_time

def get_next_day_zero_time(dt: datetime=None):
    if not dt:
        dt = datetime.now()
    one_day = timedelta(days=1)
    next_date = dt + one_day
    zero_datetime = next_date.replace(hour=0, minute=0, second=0, microsecond=0)
    return zero_datetime

def get_date_zero_time(dt: datetime=None, ret_timestamp=False):
    if not dt:
        dt = datetime.now()
    zero_datetime = dt.replace(hour=0, minute=0, second=0, microsecond=0)
    if ret_timestamp:
        zero_datetime = int(zero_datetime.timestamp() * 1000)
    return zero_datetime

def str_to_time(t_str, type=1, format=None):
    '''
    eg: yyyy-MM-dd HH:mm:ss -> %Y-%m-%d %H:%M:%S
    type = 1: %Y-%m-%d %H:M:%S
    type = 2: %Y%m%d%H%M%S
    type = 3: %Y%m%d%H%M%S%f
    type = 4: %Y-%m-%dT%H:%M:%S.%fZ
    type = 5: %Y%m%d
    type = 6: %Y%m
    '''
    if format:
        time_format = format
    else:
        if type == 1:
            time_format = "%Y-%m-%d %H:%M:%S"
        elif type == 2:
            time_format = "%Y%m%d%H%M%S"
        elif type == 3:
            time_format = "%Y%m%d%H%M%S%f"
        elif type == 4:
            time_format = "%Y-%m-%dT%H:%M:%S.%fZ"
        elif type == 5:
            time_format = "%Y%m%d"
        elif type == 6:
            time_format = "%Y%m"
        else:
            time_format = "%Y-%m-%d %H:%M:%S"
    #print('Time Format: %s' % time_format, 'debug')
    dt = datetime.strptime(t_str, time_format)
    return dt

def fmt_time(dt:datetime, type=1, format=None):
    '''
    eg: yyyy-MM-dd HH:mm:ss -> %Y-%m-%d %H:%M:%S
    type = 1: %Y-%m-%d %H:M:%S
    type = 2: %Y%m%d%H%M%S
    type = 3: %Y%m%d%H%M%S%f
    type = 4: %Y-%m-%dT%H:%M:%S.%fZ
    type = 5: %Y%m%d
    type = 6: %Y%m
    '''
    if format:
        time_format = format
    else:
        if type == 1:
            time_format = "%Y-%m-%d %H:%M:%S"
        elif type == 2:
            time_format = "%Y%m%d%H%M%S"
        elif type == 3:
            time_format = "%Y%m%d%H%M%S%f"
        elif type == 4:
            time_format = "%Y-%m-%dT%H:%M:%S.%fZ"
        elif type == 5:
            time_format = "%Y%m%d"
        elif type == 6:
            time_format = "%Y%m"
        else:
            time_format = "%Y-%m-%d %H:%M:%S"
    #print('Time Format: %s' % time_format, 'debug')
    tn = dt.strftime(time_format)
    return tn